package progetto.gruppo0506;

import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ItinerarioActivity extends Activity {
	private ArrayAdapter<String> arrayAdapter;
	private ListView listaView;
	private LinkedList<String> elenco;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.elenco_activity);
		
		listaView = (ListView) findViewById(R.id.lista_poi);
		
		//elenco di prova, sostituire con elenco itinerario
		elenco = new LinkedList<String>();
		for(int i=0; i<6; i++){
			elenco.add(""+i);
		}
		
		arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item,
				R.id.listItemTextView, elenco);
		listaView.setAdapter(arrayAdapter);
		
		registerForContextMenu(listaView);
	}
	
	
	//creazione menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.itinerario_menu, menu);
	    return true;
	}
	
	//inserimento azioni relative alle voci del menu
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.nuovo:
	    	intent = new Intent(ItinerarioActivity.this,PreferenzeUtenteActivity.class);
        	startActivity(intent);
	        return true;
	    case R.id.aggiungi:
	    	intent = new Intent(ItinerarioActivity.this,ElencoPOIActivity.class);
        	startActivity(intent);
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	//creazione context menu
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
				super.onCreateContextMenu(menu, v, menuInfo);
				MenuInflater inflater = getMenuInflater();
				inflater.inflate(R.menu.itinerario_context_menu, menu);
			}
			
			
			@Override
			public boolean onContextItemSelected(MenuItem item) {
				AdapterContextMenuInfo adapterMenuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
				int position = adapterMenuInfo.position;
				switch (item.getItemId()) {
				case R.id.elimina:
					//eliminare POI dall'itinerario nel db
					arrayAdapter.remove(arrayAdapter.getItem(position));
					return true;
				default:
			    return super.onContextItemSelected(item);
			  }
			}
}
